package org.as3search.providers {
	/*
	 * as3search - http://code.google.com/p/as3search
	 * Copyright (c) 2009-2010 by respective authors. All rights reserved.
	 *
	 * This library is free software; you can redistribute it and/or modify it under the
	 * terms of the GNU Lesser General Public License as published by the Free Software
	 * Foundation; either version 2.1 of the License, or (at your option) any later
	 * version.
	 *
	 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
	 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
	 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
	 *
	 * You should have received a copy of the GNU Lesser General Public License along
	 * with this library; if not, write to the Free Software Foundation, Inc.,
	 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
	 */
	
	import flash.errors.IllegalOperationError;
	
	import org.flexunit.Assert;
	
	/**
	 * 
	 */
	public class ArraySearchProviderTestCase {
		
		private var collection:Array;
		
		private var classToTestRef:org.as3search.providers.ArraySearchProvider;
		
		public function ArraySearchProviderTestCase() {
		
		}
		
		[Before]
		public function setup():void {
			collection = ['a','b','c'];
			classToTestRef = new ArraySearchProvider(collection);
		}
		
		[Test]
		public function testHasNext():void {
			Assert.assertTrue("should still have some left, haven't done anything yet",
				classToTestRef.hasNext());
			
			classToTestRef.next();
			classToTestRef.next();
			classToTestRef.next();
			
			Assert.assertFalse("hasNext() should return false, we've iterated through it",
				classToTestRef.hasNext());
//			collection size change
			collection.push("d");
			Assert.assertTrue("failed to return true after we've added to the collection",
				classToTestRef.hasNext());
			classToTestRef.next();
			Assert.assertFalse("failed to return false after we've reached the true end of the collection",
				classToTestRef.hasNext());
		}
		
		[Test]
		public function testNext():void {
			while (classToTestRef.hasNext()) {
				Assert.assertNotNull("next() should NEVER return null",
					classToTestRef.next());
			}
			
			try {
				classToTestRef.next();
				Assert.fail("next() did not throw an error when illegally called.");
			} catch (e:IllegalOperationError) {
//				passed!
			}
		}
		
		[Test]
		public function testReset():void {
			while (classToTestRef.hasNext()) {
				classToTestRef.next();
			}
			
			Assert.assertFalse("hasNext() should return false, we just iterated to " +
				"the end.", classToTestRef.hasNext());
			
			classToTestRef.reset();
			
			Assert.assertTrue("hasNext() should return true after we reset the iterator.",
				classToTestRef.hasNext());
		}
		
		[Test]
		public function testTarget():void {
			try {
				classToTestRef.target = 1;
				Assert.fail("set target should have thrown an ArgumentError when assigned to a Number.");
			} catch (e:ArgumentError) {
//				passed!
			}
		}
		
		[Test]
		public function testGetLength():void {
			Assert.assertEquals("'length' did not return the correct number",
				classToTestRef.length, 3);
//			now test if collection is null
			try {
				classToTestRef.target = null;
				Assert.assertEquals("'length' did not return 0 when target is null",
					classToTestRef.length, 0);
			} catch (e:TypeError) {
				Assert.fail("'length' threw an error when target is null");
			}
//			test that searchProvider.length must equal collection.length
			classToTestRef.target = collection;
			Assert.assertEquals("failed to match collection size initially",
				collection.length, classToTestRef.length);
			collection.push('d');
			Assert.assertEquals("failed to match collection size after collection size change",
				collection.length, classToTestRef.length);
		}
	}
}